package com.wgj.android.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jiaai.xui.base.XActivity;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.utils.http.entity.VUpload;
import com.jiaai.xui.utils.imageload.XImage;
import com.jiaai.xui.view.XTakePhotosDialog;
import com.jiaai.xui.view.widget.XLoadingDialog;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.utils.CacheUtils;
import com.wgj.android.widget.imageselect.ImagePicker;
import com.wgj.android.widget.imageselect.cropper.CropImage;
import com.wgj.android.widget.imageselect.cropper.CropImageView;

import org.w3c.dom.Text;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VIPInterfaceActivity extends XActivity implements XTakePhotosDialog.OnTakePhotosListener {
    @BindView(R.id.iv_bg)
    ImageView ivBg;
    @BindView(R.id.image1)
    ImageView image1;
    @BindView(R.id.image2)
    ImageView image2;
    @BindView(R.id.image3)
    ImageView image3;
    @BindView(R.id.image4)
    ImageView image4;
    @BindView(R.id.image5)
    ImageView image5;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.et_des1)
    EditText etDes1;
    @BindView(R.id.et_des2)
    EditText etDes2;
    @BindView(R.id.et_des3)
    EditText etDes3;
    @BindView(R.id.et_des4)
    EditText etDes4;
    @BindView(R.id.et_des5)
    EditText etDes5;
    private ImagePicker imagePicker;
    private ImageView image;
    private Map<String, Object> param = new HashMap<>();
    private String imgPath1;
    private String imgPath2;
    private String imgPath3;
    private String imgPath4;
    private String imgPath5;
    private String links;
    private String PerPicture;

    @Override
    public int getLayoutId() {
        return R.layout.activity_vipinterface;
    }

    @Override
    public void initData(Bundle bundle) {
        ButterKnife.bind(this);
        String merchant = getIntent().getStringExtra("merchant");
        if(!merchant.equals("")){
             JSONObject jsonObject=JSONObject.parseObject(merchant);
             links =jsonObject.getString("links");
             PerPicture =jsonObject.getString("PerPicture");
            
        }



    }

    private void setData() {
        List<String> list=new ArrayList<>();
        String str[]=links.split("\\|");
        for(int i=0;i<str.length;i++){
            String s[]=str[i].split(",");
            for(int j=0;j<s.length;j++){
                list.add(s[j]);
            }
        }
          if(links!=null){
              XImage.getInstance().load(ivBg,PerPicture);

              XImage.getInstance().load(image1,list.get(0));
              XImage.getInstance().load(image2,list.get(2));
              XImage.getInstance().load(image3,list.get(4));
              XImage.getInstance().load(image4,list.get(6));
              XImage.getInstance().load(image5,list.get(8));
              etDes1.setText(list.get(1));
              etDes2.setText(list.get(3));
              etDes3.setText(list.get(5));
              etDes4.setText(list.get(7));
              etDes5.setText(list.get(9));
          }
    }

    @Override
    public void initView() {
        if(!TextUtils.isEmpty(links)){
            setData();
        }

        imagePicker = new ImagePicker();
        // 设置标题
        imagePicker.setTitle("选择图片");
        // 设置是否裁剪图片
        imagePicker.setCropImage(true);
    }

    @OnClick({R.id.iv_bg,R.id.image1, R.id.image2, R.id.image3, R.id.image4, R.id.image5})
    public void onSelect(View v) {
        new XTakePhotosDialog(this, R.style.dialog_setting).setTakePhotosListener(this).show();
        switch (v.getId()) {
            case R.id.iv_bg:
                image = ivBg;
                break;
            case R.id.image1:
                image = image1;
                break;
            case R.id.image2:
                image = image2;
                break;
            case R.id.image3:
                image = image3;
                break;
            case R.id.image4:
                image = image4;
                break;
            case R.id.image5:
                image = image5;
                break;
        }
    }

    @OnClick(R.id.tv_submit)
    public void onSumbit(View v) {
        if (TextUtils.isEmpty(etDes1.getText())
            ||TextUtils.isEmpty(etDes2.getText())
            ||TextUtils.isEmpty(etDes3.getText())
            ||TextUtils.isEmpty(etDes4.getText())
            ||TextUtils.isEmpty(etDes5.getText())
            ||TextUtils.isEmpty(imgPath1)
            ||TextUtils.isEmpty(imgPath2)
            ||TextUtils.isEmpty(imgPath3)
            ||TextUtils.isEmpty(imgPath4)
            ||TextUtils.isEmpty(imgPath5)) {
            XToast.info("不能为空");
            return;
        }
        XLoadingDialog.with(this).show();
       /* JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(URLEncoder.encode(imgPath1,"utf-8"),etDes1.getText());
            jsonObject.put(URLEncoder.encode(imgPath2,"utf-8"),etDes2.getText());
            jsonObject.put(URLEncoder.encode(imgPath3,"utf-8"),etDes3.getText());
            jsonObject.put(URLEncoder.encode(imgPath4,"utf-8"),etDes4.getText());
            jsonObject.put(URLEncoder.encode(imgPath5,"utf-8"),etDes5.getText());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/
        String str =imgPath1+","+etDes1.getText()+"|"+imgPath2+","+etDes2.getText()+"|"+imgPath3+","+etDes3.getText()+"|"+imgPath4+","+etDes4.getText()+"|"+imgPath5+","+etDes5.getText();
        param.put("mId", CacheUtils.getMember().getM_id());
        param.put("links",str);
        XHttp.obtain().post(WGJAPI.VISITING_CARD, param, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                XToast.success("成功");
                XLoadingDialog.with(VIPInterfaceActivity.this).dismiss();
            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("失败");
                XLoadingDialog.with(VIPInterfaceActivity.this).dismiss();
            }
        });

    }


    private void uploadAvater(String path) {
        XLoadingDialog.with(this).show();
        final Map<String, File> params = new HashMap<>();
        params.put("file", new File(path));
        XHttp.obtain().upload("wgj", params, new ListHttpCallBack<VUpload>() {
            @Override
            public void onSuccess(final List<VUpload> list) {
                String path=list.get(0).getFilePath();

                XLoadingDialog.with(VIPInterfaceActivity.this).dismiss();
                XImage.getInstance().load(image,path);
                if (image == ivBg) {
                    param.put("perPicture",path);
                }
                if(image==image1){
                    imgPath1=path;
                }
                if(image==image2){
                    imgPath2=path;
                }
                if(image==image3){
                    imgPath3=path;
                }
                if(image==image4){
                    imgPath4=path;
                }
                if(image==image5){
                    imgPath5=path;
                }
                XToast.success("修改成功");
            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("上传图片失败");
                XLoadingDialog.with(VIPInterfaceActivity.this).dismiss();
            }
        });
    }

    @Override
    public void onTakePictureClick() {
        imagePicker.startCamera(this, new ImagePicker.Callback() {
            @Override
            public void onPickImage(Uri imageUri) {

            }

            // 自定义裁剪配置
            @Override
            public void cropConfig(CropImage.ActivityBuilder builder) {
                builder
                        // 是否启动多点触摸
                        .setMultiTouchEnabled(false)
                        // 设置网格显示模式
                        .setGuidelines(CropImageView.Guidelines.OFF)
                        // 圆形/矩形
                        .setCropShape(CropImageView.CropShape.RECTANGLE)
                        // 调整裁剪后的图片最终大小
                        .setRequestedSize(360, 360)
                        // 宽高比
                        .setAspectRatio(1, 1);
            }

            @Override
            public void onCropImage(Uri imageUri) {
                uploadAvater(imageUri.getPath());

            }
        });
    }

    @Override
    public void onChoosePictureClick() {
        imagePicker.startGallery(this, new ImagePicker.Callback() {
            @Override
            public void onPickImage(Uri imageUri) {
            }

            // 自定义裁剪配置
            @Override
            public void cropConfig(CropImage.ActivityBuilder builder) {
                builder
                        // 是否启动多点触摸
                        .setMultiTouchEnabled(false)
                        // 设置网格显示模式
                        .setGuidelines(CropImageView.Guidelines.OFF)
                        // 圆形/矩形
                        .setCropShape(CropImageView.CropShape.RECTANGLE)
                        // 调整裁剪后的图片最终大小
                        .setRequestedSize(360, 360)
                        // 宽高比
                        .setAspectRatio(1, 1);
            }

            @Override
            public void onCropImage(Uri imageUri) {
                uploadAvater(imageUri.getPath());
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        imagePicker.onActivityResult(this, requestCode, resultCode, data);
    }
}
